import VueRouter from "vue-router";
import store from "@/store";

import MyLayout from "@/layout/MyLayout.vue";
import MyHome from "@/layout/MyHome.vue";
import MyUser from "@/layout/system/MyUser.vue";
import MyRole from "@/layout/system/MyRole.vue";
import MyMenu from "@/layout/system/MyMenu.vue";
import MyArticle from "@/layout/content/MyArticle.vue";
import MyCategory from "@/layout/content/MyCategory.vue";
import MyComment from "@/layout/content/MyComment.vue";
import ArticleDetail from "@/layout/content/ArticleDetail.vue";
import MyLogin from "@/layout/MyLogin.vue";
import AccessData from "@/layout/statistics/AccessData.vue";
import ContentData from "@/layout/statistics/ContentData.vue";
import InteractionData from "@/layout/statistics/InteractionData.vue";
import UserData from "@/layout/statistics/UserData.vue";

const router = new VueRouter({
  routes: [
    {
      path: "/",
      component: MyLayout,
      children: [
        {
          path: "",
          component: MyHome,
          meta: { requiresAuth: true }, // 需要认证才能访问
        },
        // 用户管理
        {
          path: "/user",
          component: MyUser,
          meta: { requiresAuth: true }, // 需要认证才能访问
        },
        {
          path: "/role",
          component: MyRole,
          meta: { requiresAuth: true }, // 需要认证才能访问
        },
        {
          path: "/menu",
          component: MyMenu,
          meta: { requiresAuth: true }, // 需要认证才能访问
        },
        // 内容管理
        {
          path: "/article",
          component: MyArticle,
          meta: { requiresAuth: true }, // 需要认证才能访问
        },
        {
          path: "/category",
          component: MyCategory,
          meta: { requiresAuth: true }, // 需要认证才能访问
        },
        {
          path: "/comment",
          component: MyComment,
          meta: { requiresAuth: true }, // 需要认证才能访问
        },
        {
          path: "/accessData",
          component: AccessData,
          meta: { requiresAuth: true }, // 需要认证才能访问
        },
        {
          path: "/contentData",
          component: ContentData,
          meta: { requiresAuth: true }, // 需要认证才能访问
        },
        {
          path: "/interactionData",
          component: InteractionData,
          meta: { requiresAuth: true }, // 需要认证才能访问
        },
        {
          path: "/userData",
          component: UserData,
          meta: { requiresAuth: true }, // 需要认证才能访问
        },
      ],
    },
    {
      path: "/login",
      component: MyLogin,
    },
    {
      path: "/articleDetail/:id",
      component: ArticleDetail,
    },
  ],
});

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 检查目标路由是否需要认证
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    // console.log('需要认证')
    // console.log('是否登录：',store.getters['user/isAuthenticated'])
    // 检查Vuex中的登录状态
    if (!store.getters["user/isAuthenticated"]) {
      // 如果未登录，跳转到登录页，并记录想要去的页面路径
      console.log("未登录，跳转到登录页");
      next({
        path: "/login",
        query: { redirect: to.fullPath },
      });
    } else {
      // 如果已登录，正常放行
      next();
    }
  } else {
    // 对于不需要认证的路由，直接放行
    console.log("不需要认证");
    next();
  }
});

export default router;
